package com.xa8bit.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lingwh
 * @desc 配置放行文件（包括war包形式部署在tomcat下的放行文件）
 * @date 2025/7/10 17:45
 */
@Configuration
public class WebSecurityConfig implements WebMvcConfigurer {

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //存放拦截器放行的的路径
        List<String> excludePathPatterns = new ArrayList<>();
        excludePathPatterns.add("/");
        excludePathPatterns.add("/index.html");
        excludePathPatterns.add("/assets/*.css");
        excludePathPatterns.add("/assets/*.js");
        excludePathPatterns.add("/assets/*.jpg");
        excludePathPatterns.add("/user/login");
        excludePathPatterns.add("/user/logout");
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
    }

}
